package cn.toguide.ffmpeg.component;

import cn.toguide.ffmpeg.config.FFMpegSDKProperties;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.UUID;

@Slf4j
@Component
public class ResourceManager {

    @Resource
    private FFMpegSDKProperties ffmpegCommandProperties;

    String makeFile(String suffix) {
        String swapPath = ffmpegCommandProperties.getSwapPath();
        if (StringUtils.hasText(swapPath)) {
            return swapPath + File.separator + UUID.randomUUID() + "." + suffix;
        }

        try {
            File tempFile = File.createTempFile(UUID.randomUUID().toString(), "." + suffix);
            return tempFile.getAbsolutePath();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void removeFile(String filePath) {
        try {
            Files.deleteIfExists(Paths.get(filePath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
